package project.data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

//import events.antEvents.PheromonesDroppedEvent;
import project.layer.agents.TruckAgent;
import project.layer.physical.Road;


public class DataHolder implements EvaporableDataHolder{


	
	private final Map<Road,FeasibilityDataStack> feasibilityDataStacks;

	public DataHolder(){
		this.feasibilityDataStacks = new HashMap<Road,FeasibilityDataStack>();
		
		
	}

	
	public Map<Road, FeasibilityDataStack> getFeasibilityDataContainer() {
		return feasibilityDataStacks;
	}
	
	
	
	public List<Integer> getFeasiblityOriginIDs(){
		ArrayList<Integer> result = new ArrayList<Integer>();
		for(Road road: feasibilityDataStacks.keySet()){
			FeasibilityDataStack stack = feasibilityDataStacks.get(road);
			for(FeasibilityData data : stack.getFeasDataPickUp()){
				if(!data.hasEvaporated()){
					result.add(data.getOriginPackageId());
				}
				
			}
		}
		return result;
	}
	
	public Road getRoadWithMostFeasibilityPheromones(List<Integer> destinationIDsToBeIgnored, int destinationID){
		if(destinationIDsToBeIgnored.contains(destinationID)){
			return null;
		}
		Road result = null;
		Iterator<Road> it = getFeasibilityDataContainer().keySet().iterator();
		while(it.hasNext()){
			Road road = it.next();
			if(getFeasibilityDataContainer().get(road).getRemainingSmellFromPackage(destinationID) > 1){
				if(result == null )
					result = road;
				if(getFeasibilityDataContainer().get(road).getRemainingSmellFromPackage(destinationID) > getFeasibilityDataContainer().get(result).getRemainingSmellFromPackage(destinationID)){
					result = road;
				}
			}
		}
		
		return result;
	}
	
	public Road getRoadWithMostFeasibilityPheromones(List<Integer> destinationIDsToBeIgnored){
		Road result = null;
		Iterator<Road> it = getFeasibilityDataContainer().keySet().iterator();
		while(it.hasNext()){
			Road road = it.next();
			if(getFeasibilityDataContainer().get(road).getHighestPickUpPhermoneCount(destinationIDsToBeIgnored) > 1){
				if(result == null )
					result = road;
				if(getFeasibilityDataContainer().get(road).getHighestPickUpPhermoneCount(destinationIDsToBeIgnored) > getFeasibilityDataContainer().get(result).getHighestPickUpPhermoneCount(destinationIDsToBeIgnored)){
					result = road;
				}
			}
		}
		
		return result;
	}


	
	public void dropFeasibilityAntPheromones(Road road, int packageId, boolean isFromPickUpPoint, double value){
//		System.out.println("Road: " + road);
		if(road != null){
			getFeasibilityDataContainer().get(road).dropPheromones(packageId, isFromPickUpPoint, value);
//			EventBroker.getEventBroker().notifyAll(new PheromonesDroppedEvent(this.getPosition()));
		}
	}



	/**
	 * This method is only used for the removal of ExplorationData. Removal (evaporation) of FeasibilityData is managed at the FeasibilityDataStack level.
	 */
	@Override
	public void remove(EvaporableData evaporatedData) {
		//Exploration Data does not need to be removed
		
	}
	
	
	
	
}
